import 'package:flutter_douban/model/note_model.dart';
import 'package:sqflite/sqlite_api.dart';
import 'base_db_provider.dart';

class NoteDbProvider extends BaseDbProvider {
  /// 表名
  final String name = 'NoteInfo';

  /// 笔记id
  final String noteId = "id";

  /// 图片链接
  final String image_u = "image_u";

  /// 笔记描述
  final String title_name = "title";

  /// 用户图片
  final String p = "p";

  /// 用户名字
  final String n = "n";

  /// 收藏数量
  final String like_count = "like_count";

  /// 播放url
  final String video_url = "video_url";

  /// 收藏用户电话
  final String phoneNum = "phoneNum";

  /// 收藏时间
  final String time = "time";

  NoteDbProvider();
  @override
  tableName() {
    return name;
  }

  /// 创建表
  @override
  createTableString() {
    return '''
        create table $name (
        $noteId integer primary key,$image_u text not null,
        $title_name text not null,$p text not null,$n text not null,$like_count integer,$video_url text,$phoneNum text not null,$time bigint)
      ''';
  }

  /// 查询数据库
  Future _getNoteProvider(Database db, int id,String phone) async {
    List<Map<String, dynamic>> maps =
        await db.rawQuery("select * from $name where $noteId = $id AND $phoneNum = $phone");
    return maps;
  }

  /// 插入到数据库
  Future insert(NoteModel model,String phone) async {
    Database db = await getDataBase();
    var noteProvider = await _getNoteProvider(db, int.parse(model.id!),phone);
    if (noteProvider != null) {
      /// 删除数据
      await db.delete(name, where: "$noteId = ? AND $phoneNum = ?", whereArgs: [model.id,phone]);
    }
    return await db.rawInsert(
        "insert into $name ($noteId,$image_u,$title_name,$p,$n,$like_count,$video_url,$phoneNum,$time) values (?,?,?,?,?,?,?,?,?)",
        [
          int.parse(model.id!),
          model.image_u,
          model.title,
          model.author!.p,
          model.author!.n,
          model.like_count,
          model.video_url,
          phone,
          /// 当前时间戳
          DateTime.now().millisecondsSinceEpoch,
        ]);
  }

  /// 删除到数据库
  Future delete(int id,String phone) async {
    Database db = await getDataBase();
    var noteProvider = await _getNoteProvider(db, id,phone);
    if (noteProvider != null) {
      /// 删除数据
      await db.delete(name, where: "$noteId = ? AND $phoneNum = ?", whereArgs: [id,phone]);
    }
    return await db.rawDelete(
      "delete from $name where $noteId = $id AND $phoneNum = $phone",
    );
  }

  /// 获取表中符合用户的全部数据
  Future<List<NoteModel>> getAllNoteInfo(String phone) async {
    Database db = await getDataBase();
    List<Map<String, dynamic>> maps = await db.rawQuery("select * from $name where $phoneNum = $phone order by $time desc");
    NoteModelList noteModelList = NoteModelList.fromJson(maps);
    return noteModelList.list;
  }


// /// 更新数据库
  // Future<void> update(NoteModel model) async {
  //   Database database = await getDataBase();
  //   await database.rawUpdate(
  //       "update $name set $image_u = ?,$title_name = ?,$p = ?,$n = ?,$like_count = ?,$video_url = ? where $noteId= ?",
  //       [
  //         model.image_u,
  //         model.title,
  //         model.author!.p,
  //         model.author!.n,
  //         model.like_count,
  //         model.video_url,
  //         int.parse(model.id!)
  //       ]);
  // }

}
